package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 统计指定时间内的营业额统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDataUtilMethod(begin, end);
        List<Double> turnoverAomuntList = new ArrayList<>();
        // 查询订单表中完成的订单营业
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Integer status = Orders.COMPLETED;
            Double amount = orderMapper.selectOrderMoney(beginTime, endTime, status);
            amount = amount == null ? 0.0 : amount;
            turnoverAomuntList.add(amount);
        }

        return TurnoverReportVO
                .builder()
                .dateList(StringUtils.join(dateList, ","))
                .turnoverList(StringUtils.join(turnoverAomuntList, ",")).build();
    }

    /**
     * 用户统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDataUtilMethod(begin, end);
        // 存放每天的新用户亮 select count(id) from user where create_time < endTime and create_time > beginTime
        List<Integer> newUserList = new ArrayList<>();
        // 存放每天的用户总量 select count(id) from user where create_time < endTime
        List<Integer> userTotalList = new ArrayList<>();

        // 统计总的用户数量
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            // 查每天用户总量
            Integer userCount = userMapper.selectUserTotalByTime(null, endTime);
            Integer newUserCount = userMapper.selectUserTotalByTime(beginTime, endTime);
            userCount = userCount == null ? 0 : userCount;
            newUserCount = newUserCount == null ? 0 : newUserCount;
            userTotalList.add(userCount);
            newUserList.add(newUserCount);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(userTotalList, ","))
                .build();
    }

    /**
     * 订单统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = getDataUtilMethod(begin, end);
        // 订单数列表
        List<Integer> orderCountList = new ArrayList<>();
        // 有效订单数
        List<Integer> validOrderCountList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            // 每天订单数
            Integer orderCount = orderMapper.selectOrderCountByStatusAndTime(beginTime, endTime, null);
            // 每天有效订单数
            Integer status = Orders.COMPLETED;  // 已完成订单
            Integer validOrderCount = orderMapper.selectOrderCountByStatusAndTime(beginTime, endTime, status);
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }
        Integer totalOrderCount = orderCountList.stream().mapToInt(Integer::intValue).sum(); // 得到所有订单总数
        Integer totalValidOrderCount = validOrderCountList.stream().mapToInt(Integer::intValue).sum(); // 所有有效订单数
        // 订单完成率
        Double orderCompletionRate = totalValidOrderCount / (double) totalOrderCount;
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList, ",")) // 日期列表
                .orderCountList(StringUtils.join(orderCountList, ",")) // 订单列表
                .validOrderCountList(StringUtils.join(validOrderCountList, ",")) // 有效订单列表
                .orderCompletionRate(orderCompletionRate) // 订单完成率
                .validOrderCount(totalValidOrderCount) // 有效订单数
                .totalOrderCount(totalOrderCount) // 订单总数
                .build();
    }

    /**
     * 销量排名10
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO topTen(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> list = orderDetailMapper.selectTop10ByTimeAndStatus(beginTime, endTime, Orders.COMPLETED);
        List<String> names = list.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        List<Integer> number = list.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(names, ","))
                .numberList(StringUtils.join(number, ","))
                .build();
    }

    private List<LocalDate> getDataUtilMethod(LocalDate begin, LocalDate end) {
        // 存放begin - end 日期之间的每天日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            // 计算指定日期后一天
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        return dateList;
    }
}
